import csv
import random
import sys, os
import time
import datetime
import pdb

word_dic = {}
word_list = []

data_file = "data2.csv"
if __name__ == "__main__":
    global word_dic, word_list

    f = open(data_file)
    reader = csv.reader(f)
    for word, meaning, level, numquestion, numwrong in reader:
        word = word.upper()
        word_dic[word] = (meaning, int(level), int(numquestion), int(numwrong))
        word_list.append(word)
    f.close()

    difficult_words = filter(lambda x: word_dic[x.upper()][1] > 2, word_list )

    file_wrong = open("wrong_words.txt")
    file_boksp = open("boksp.txt", "w")
    prev_word = ""
    wrong_list = []
    for word in file_wrong:
        word = word.strip().upper()
        if word != "":
            wrong_list.append(word)

    for word in wrong_list:
        while not word_dic.has_key(word):
            print "unable to find the key %s. (%d/%d)"%(word,wrong_list.index(prev_word), len(wrong_list))
            word = raw_input("Enter (q if cancel, c if continue) :")
            word = word.upper()
            if word == "Q":
                break
            if word == "C":
                break
        
        if word == "Q":
            break
        
        prev_word = word

        if not word_dic.has_key(word):
            continue
        
        level = word_dic[word][1]
        # add level for wrong words ( 1 for high level, 2 for level2, 3 for level1)
        if level > 3:
            word_dic[word] = ( word_dic[word][0], word_dic[word][1] + 1, word_dic[word][2], word_dic[word][3] + 1)
        else:
            word_dic[word] = ( word_dic[word][0], 4, word_dic[word][2], word_dic[word][3] + 1)

        file_boksp.write("%s %s\n" % (word, word_dic[word][0]))

        # remove difficult & wrong words to make the list contain only 
        # difficult but right words
        if difficult_words.__contains__(word):
            difficult_words.remove(word)
            
    
    f_problem = open("problem.txt", "r")
    f_problem.readline()
    f_problem.readline()
    first_problem = f_problem.readline()
    f_problem.close()
    first_problem = first_problem.split()[1]
    raw_input("First problem: %s"%first_problem)

    f_log = open("last_log.txt", "r")
    last_log = f_log.readline().split()
    f_log.close()

    if last_log[7] == first_problem:
        if (raw_input("Want to update again? (y/n)").upper() != "Y"):
            sys.exit(0)
            
    f_log = open("last_log.txt", "w")
    print "log file...: "
    print last_log
    for i in range( 0, len(last_log) -1):
        f_log.write("%s "%last_log[i])
        print last_log[i]
    f_log.write(first_problem)
    f_log.close()

    # difficult but right words
    for word in difficult_words:
        word_dic[word] = ( word_dic[word][0], word_dic[word][1] - 1, word_dic[word][2], word_dic[word][3])

    writer = csv.writer(open(data_file, "w"))
    for word in word_list:
        temp_item = ( word, word_dic[word][0], str(word_dic[word][1]), str(word_dic[word][2]), str(word_dic[word][3]) )
        writer.writerow( temp_item )

    file_boksp.close()

    os.system("boksp.txt")
